﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CountTools
{
   public class TreeViewCheck
    {
        /// <summary>
        /// 系列节点 Checked 属性控制
        /// </summary>
        /// <param name="e"></param>
        public static void CheckControl(TreeViewEventArgs e)
        {
            if (e.Action != TreeViewAction.Unknown)
            {
                if (e.Node != null)
                {
                    CheckParentNode(e.Node, e.Node.Checked);
                }
                if (e.Node.Nodes.Count > 0)
                {
                    CheckAllChildNodes(e.Node, e.Node.Checked);
                }
            }
        }
        #region 
        //改变所有子节点的状态
        public static void CheckAllChildNodes(TreeNode node, bool IsChecked)
        {
            foreach (TreeNode tn in node.Nodes)
            {
                tn.Checked = IsChecked;
                if (tn.Nodes.Count > 0)
                {
                    CheckAllChildNodes(tn, IsChecked);
                }
            }
            FooParent(node);
        }

        public static void CheckAllChildNodes(TreeNode node)
        {
            foreach (TreeNode tn in node.Nodes)
            {
                tn.Checked = !node.Checked;
                if (tn.Nodes.Count > 0)
                {
                    CheckAllChildNodes(tn);
                }
            }
            FooParent(node);
        }


        //改变父节点的选中状态
        public static void CheckParentNode(TreeNode curNode, bool IsChecked)
        {
            bool bChecked = true;
            if (curNode.Parent != null)
            {
                foreach (TreeNode node in curNode.Parent.Nodes)
                {
                    if (node.Checked == false)
                    {
                        bChecked = false;
                        break;
                    }
                }
                if (bChecked)
                {
                    curNode.Parent.Checked = true;
                    CheckParentNode(curNode.Parent, true);
                }
                else
                {
                    curNode.Parent.Checked = false;
                    CheckParentNode(curNode.Parent, false);
                }
            }
            FooParent(curNode);
        }

        /// <summary>
        /// 处理父节点，子节点的选中
        /// </summary>
        /// <param name="node">需要选中的树</param>
        /// <param name="isSetParentState">是否选中父节点</param>
        /// <param name="state">选中/不选中</param>
        public static void SetTreeNodeCheckBoxState(TreeNode node, bool isSetParentState, bool state)
        {
            if (node == null)
                return;
            node.Checked = state;
            if (isSetParentState && node.Parent != null && node.Checked == true)
            {
                SetTreeNodeCheckBoxState(node.Parent, isSetParentState, state);
            }
            else if (isSetParentState && node.Parent != null && node.Checked == false)
            {
                //处理取消选中
                bool isSelect = false;
                foreach (TreeNode n in node.Parent.Nodes)
                {
                    if (n.Checked == true)
                        isSelect = true;
                }
                //平级都未有选中的才取消选中
                if (isSelect == false)
                    SetTreeNodeCheckBoxState(node.Parent, isSetParentState, state);
            }
            else if (!isSetParentState && node.Nodes != null && node.Nodes.Count > 0)
            {
                foreach (TreeNode node2 in node.Nodes)
                {
                    SetTreeNodeCheckBoxState(node2, isSetParentState, state);
                }
            }
            FooParent(node);
        }
        #endregion

        private static void FooParent(TreeNode root)
        {
            if (root.Parent != null)
            {
                //兄弟节点被选的个数
                int brotherNodeCheckedCount = 0;
                //遍历该节点的兄弟节点
                foreach (TreeNode node in root.Parent.Nodes)
                {
                    if (node.Checked == true)
                        brotherNodeCheckedCount += 1;
                }
                //兄弟节点全没选
                if (brotherNodeCheckedCount == 0)
                {
                    TreeNode parentNode = root.Parent;
                    parentNode.Checked = false;//其父节点也不选
                    FooParent(parentNode);
                }
                //兄弟节点中只要有一个被选
                if (brotherNodeCheckedCount > 0)
                {
                    TreeNode parentNode = root.Parent;
                    parentNode.Checked = true;
                    FooParent(parentNode);//其父节点也被选
                }
            }
        }

    }
}